import 'package:flutter/material.dart';
import 'package:flutter_wan_android/core/config/router.dart';
import 'package:flutter_wan_android/core/widget/loading_dialog.dart';
import 'package:flutter_wan_android/feature/core/ui_state.dart';
import 'package:flutter_wan_android/feature/user/login_view_model.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

import '../../generated/assets.dart';
import '../../core/widget/custom_app_bar.dart';
import 'user_text_field.dart';

class LoginScreen extends StatefulWidget {
  const LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  final TextEditingController _usernameController = TextEditingController();

  final TextEditingController _passwordController = TextEditingController();
  late final LoginViewModel _viewModel;

  @override
  void initState() {
    super.initState();
    _viewModel = context.read<LoginViewModel>();
    _viewModel.addListener(_viewModelChange);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(title: "登录"),
      body: SizedBox.expand(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              _buildLogo(),
              UserTextField(hintText: "请输入账号", controller: _usernameController),
              SizedBox(height: 20),
              UserTextField(hintText: "请输入密码", controller: _passwordController, obscureText: true),
              _buildGoRegister(context),
              SizedBox(height: 20),
              _buildLoginButton(context),
            ],
          ),
        ),
      ),
    );
  }

  SizedBox _buildLoginButton(BuildContext context) {
    return SizedBox(
      width: 200,
      child: ElevatedButton(
        onPressed: _login,
        style: ButtonStyle(
          backgroundColor: WidgetStatePropertyAll(
            Theme.of(context).primaryColor,
          ),
        ),
        child: Text("登录", style: TextStyle(color: Colors.white, fontSize: 16)),
      ),
    );
  }

  Align _buildGoRegister(BuildContext context) {
    return Align(
      alignment: Alignment.topRight,
      child: TextButton(
        style: ButtonStyle(
          padding: WidgetStateProperty.all(EdgeInsets.only(right: 0)),
        ),
        onPressed: () {
          Navigator.pushNamed(context, AppPages.register);
        },
        child: Text("去注册"),
      ),
    );
  }

  Padding _buildLogo() {
    return Padding(
      padding: EdgeInsets.only(top: 80, bottom: 40),
      child: Image(width: 200, image: AssetImage(Assets.imagesLogo)),
    );
  }


  void _login() {
    final String account = _usernameController.text;
    final String password = _passwordController.text;
    context.read<LoginViewModel>().login(account, password);
  }

  void _viewModelChange() {
    final viewModel = context.read<LoginViewModel>();
    final uiState = viewModel.uiState;
    if (uiState is! Loading) {
      LoadingDialog.hide(context);
    }
    switch (uiState) {
      case Loading():
        LoadingDialog.show(context);
      case Success():
        Fluttertoast.showToast(msg: "登录成功");
        Navigator.pushNamedAndRemoveUntil(
          context,
          AppPages.home,
          (route) => false,
        );
      case Error():
        Fluttertoast.showToast(msg: viewModel.message);
    }
  }

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    _viewModel.removeListener(_viewModelChange);
    super.dispose();
  }
}
